﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Threading;
using System.Collections;
using System.Collections.ObjectModel;
using System.Windows.Media.Animation;

namespace AppDemo
{
    public class PopupMenu : ItemsControl
    {
        static PopupMenu()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(PopupMenu), new FrameworkPropertyMetadata(typeof(PopupMenu)));
        }


        public static MenuItemCollection GetHost(DependencyObject obj)
        {
            return (MenuItemCollection)obj.GetValue(HostProperty);
        }

        public static void SetHost(DependencyObject obj, MenuItemCollection value)
        {
            obj.SetValue(HostProperty, value);
        }

        public static Popup _popup;
        public static bool animateFinish = true;
        public static UIElement tempuielement = null;
        public static readonly DependencyProperty HostProperty =
            DependencyProperty.RegisterAttached("Host", typeof(MenuItemCollection), typeof(PopupMenu),
            new UIPropertyMetadata((o, e) =>
            {
                var element = o as FrameworkElement;

                if (_popup == null)
                {
                    var host = e.NewValue as MenuItemCollection;
                    _popup = new Popup();
                    _popup.AllowsTransparency = true;
                    //_popup.PopupAnimation = PopupAnimation.Fade;
                   _popup.Placement = PlacementMode.Right;

                    var menu = new PopupMenu();
                    menu.ItemsSource = host;
                    _popup.Child = menu;
                    _popup.Opacity = 1;
                }
                element.MouseEnter += new MouseEventHandler((sender, a) =>
                {
                    tempuielement = sender as FrameworkElement;
                    if (_popup.IsOpen)
                    {
                        if (animateFinish)
                        {
                            var opacityLow = new DoubleAnimation(1, 0, new Duration(TimeSpan.FromSeconds(0.5)));
                            opacityLow.Completed += delegate
                            {
                                animateFinish = true;
                                _popup.IsOpen = false;
                                _popup.PlacementTarget = tempuielement;

                                var opacityHigh = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromSeconds(0.5)));
                                _popup.IsOpen = true;
                                _popup.Child.BeginAnimation(FrameworkElement.OpacityProperty, opacityHigh);
                            };
                            _popup.Child.BeginAnimation(FrameworkElement.OpacityProperty, opacityLow);
                            animateFinish = false;
                        }
                    }
                    else
                    {
                        _popup.IsOpen = false;
                        _popup.PlacementTarget = sender as FrameworkElement;
                        var opacityHigh = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromSeconds(0.5)));
                        _popup.IsOpen = true;
                        _popup.Child.Opacity = 0;
                        _popup.Child.BeginAnimation(FrameworkElement.OpacityProperty, opacityHigh);
                    }
                 
                });
              
                _popup.MouseLeave += delegate
                {
                    if (!element.IsMouseOver)
                    {
                        var opacityLow = new DoubleAnimation(1, 0, new Duration(TimeSpan.FromSeconds(0.5)));
                        opacityLow.Completed += delegate
                        {
                            animateFinish = true;
                            _popup.IsOpen = false;
                           
                        };
                        _popup.Child.BeginAnimation(FrameworkElement.OpacityProperty, opacityLow);
                        animateFinish = false;
                    }
                };
            }));
    }

    public class MenuItemCollection : Collection<MenuItem>
    {
    }
}
